package cn.ming.wfchat.app.moment.third.beans;

import android.content.Context;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;

import cn.ming.wfchat.app.moment.third.Constants;
import cn.ming.wfchat.app.moment.third.utils.SpanUtils;
import cn.ming.wfchat.app.moment.third.utils.Utils;
import cn.ming.wfchat.kit.mm.MediaEntry;
import cn.wildfirechat.model.UserInfo;
import cn.wildfirechat.remote.ChatManager;

public class FriendCircleBean {

    private long id;
    private int viewType;

    private String content;

    private List<CommentBean> commentBeans;

    private List<PraiseBean> praiseBeans;

    private List<MediaEntry> mediaEntries;

    private UserBean userBean;

    private OtherInfoBean otherInfoBean;


    private String portrait;
    private String hash;
    private String link;
    private String displayName;
    private boolean privateOwner;

    public boolean isPrivateOwner() {
        return privateOwner;
    }

    public void setPrivateOwner(boolean privateOwner) {
        this.privateOwner = privateOwner;
    }

    public static FriendCircleBean fromFeed(Context context, DynamicsBean feed) {
        FriendCircleBean friendCircleBean = new FriendCircleBean();
        friendCircleBean.id = feed.getId();
        friendCircleBean.hash = feed.getHash();
        friendCircleBean.link = feed.getLink();
        friendCircleBean.displayName = feed.getDisplayName();
        friendCircleBean.portrait = feed.getPortrait();
        friendCircleBean.privateOwner = feed.isPrivateOwner();

        UserBean userBean = new UserBean();

        if (TextUtils.isEmpty(feed.getLink())) {
            if (feed.getResourcesList() == null) {
                //纯文本
                friendCircleBean.viewType = Constants.FriendCircleType.FRIEND_CIRCLE_TYPE_ONLY_WORD;
            } else {
                //文字和图片
                friendCircleBean.viewType = Constants.FriendCircleType.FRIEND_CIRCLE_TYPE_WORD_AND_IMAGES;
            }
        } else {
            //分享链接
            friendCircleBean.viewType = Constants.FriendCircleType.FRIEND_CIRCLE_TYPE_WORD_AND_URL;
        }
        UserInfo userInfo = ChatManager.Instance().getUserInfo(feed.getUserId() + "", false);
        userBean.setUserId(feed.getUserId() + "");
        userBean.setUserAvatarUrl(userInfo.portrait);
        if (!TextUtils.isEmpty(userInfo.groupAlias)) {
            userBean.setUserName(userInfo.groupAlias);
        } else if (!TextUtils.isEmpty(userInfo.friendAlias)) {
            userBean.setUserName(userInfo.friendAlias);
        } else if (!TextUtils.isEmpty(userInfo.displayName)) {
            userBean.setUserName(userInfo.displayName);
        } else {
            userBean.setUserName(userInfo.name);
        }

        friendCircleBean.userBean = userBean;
        friendCircleBean.setContent(feed.getContent());
        //点赞
        if (feed.getLikeUserIdList() != null && feed.getLikeUserIdList().size() > 0) {
            List<PraiseBean> praiseBeans = new ArrayList<>();
            for (Integer integer : feed.getLikeUserIdList()) {
                PraiseBean praiseBean = new PraiseBean();
                UserInfo likeuserInfo = ChatManager.Instance().getUserInfo(integer + "", false);
                praiseBean.setPraiseUserId(integer + "");
                praiseBean.setPraiseUserName(likeuserInfo.displayName);
                praiseBeans.add(praiseBean);

            }
            friendCircleBean.praiseBeans = praiseBeans;
            friendCircleBean.setPraiseSpan(SpanUtils.makePraiseSpan(context, praiseBeans, null));
        }
/**
 * 评论
 */
        if (feed.getDynamicsCommentsUserList() != null && feed.getDynamicsCommentsUserList().size() > 0) {
            List<CommentBean> commentBeans = new ArrayList<>();
            for (DynamicsBean.DynamicsCommentsUserListBean comment : feed.getDynamicsCommentsUserList()) {
                CommentBean commentBean = new CommentBean();
                if (TextUtils.isEmpty(comment.getParentCommentUserId())) {
                    commentBean.setCommentType(Constants.CommentType.COMMENT_TYPE_SINGLE);
                    commentBean.setChildUserId(comment.getCommentUserId() + "");
                    commentBean.setChildUserName(comment.getCommentUserName());
                } else {
                    commentBean.setCommentType(Constants.CommentType.COMMENT_TYPE_REPLY);
                    commentBean.setChildUserId(comment.getCommentUserId() + "");
                    commentBean.setChildUserName(comment.getCommentUserName());
                    commentBean.setParentUserId(comment.getParentCommentUserId());
                    commentBean.setParentUserName(comment.getParentCommentUserName());
                }
                commentBean.setId(comment.getId());
                commentBean.setCommentContent(comment.getContent());
                commentBeans.add(commentBean);
            }
            friendCircleBean.commentBeans = commentBeans;
        }
        /**
         * 视频or图片
         */
        if (feed.getResourcesList() != null && feed.getResourcesList().size() > 0) {
            List<MediaEntry> mediaEntries = new ArrayList<>();
            for (DynamicsBean.ResourcesListBean entry : feed.getResourcesList()) {
                MediaEntry mediaEntry = new MediaEntry();
                if (entry.getResourceType().equals("VIDEO")) {
                    mediaEntry.setType(MediaEntry.TYPE_VIDEO);
                } else {
                    mediaEntry.setType(MediaEntry.TYPE_IMAGE);
                }
                mediaEntry.setMediaUrl(entry.getResourceSrc());
                mediaEntry.setThumbnailUrl(entry.getResourceSrc());
                mediaEntries.add(mediaEntry);
            }
            friendCircleBean.mediaEntries = mediaEntries;
        }
        /**
         * 时间
         */
        OtherInfoBean infoBean = new OtherInfoBean();
        infoBean.setTime(feed.getCreateTime());
        friendCircleBean.otherInfoBean = infoBean;

        return friendCircleBean;
    }

    public String getHash() {
        return hash;
    }

    public void setHash(String hash) {
        this.hash = hash;
    }

    private boolean isExpanded;

    private boolean isShowCheckAll;

    public boolean isExpanded() {
        return isExpanded;
    }

    public void setExpanded(boolean expanded) {
        isExpanded = expanded;
    }

    public boolean isShowCheckAll() {
        return isShowCheckAll;
    }

    public void setShowCheckAll(boolean showCheckAll) {
        isShowCheckAll = showCheckAll;
    }

    public boolean isShowComment() {
        return commentBeans != null && commentBeans.size() > 0;
    }

    public boolean isShowPraise() {
        return praiseBeans != null && praiseBeans.size() > 0;
    }

    public OtherInfoBean getOtherInfoBean() {
        return otherInfoBean;
    }

    public void setOtherInfoBean(OtherInfoBean otherInfoBean) {
        this.otherInfoBean = otherInfoBean;
    }

    public void setUserBean(UserBean userBean) {
        this.userBean = userBean;
    }

    public UserBean getUserBean() {
        return userBean;
    }

    public int getViewType() {
        return viewType;
    }

    public void setViewType(int viewType) {
        this.viewType = viewType;
    }

    public String getContent() {
        return content;
    }

    public SpannableStringBuilder getContentSpan() {
        return contentSpan;
    }

    public void setContentSpan(SpannableStringBuilder contentSpan) {
        this.contentSpan = contentSpan;
        this.isShowCheckAll = Utils.calculateShowCheckAllText(contentSpan.toString());
    }

    private SpannableStringBuilder contentSpan;


    public void setContent(String content) {
        this.content = content;
        if (!TextUtils.isEmpty(content)) {
            setContentSpan(new SpannableStringBuilder(content));
        }
    }

    public List<CommentBean> getCommentBeans() {
        return commentBeans;
    }

    public void setCommentBeans(List<CommentBean> commentBeans) {
        this.commentBeans = commentBeans;
    }

    public List<PraiseBean> getPraiseBeans() {
        return praiseBeans;
    }

    public void setPraiseBeans(List<PraiseBean> praiseBeans) {
        this.praiseBeans = praiseBeans;
    }


    public List<MediaEntry> getMediaEntries() {
        return mediaEntries;
    }

    public void setMediaEntries(List<MediaEntry> mediaEntries) {
        this.mediaEntries = mediaEntries;
    }


    public void setPraiseSpan(SpannableStringBuilder praiseSpan) {
        this.praiseSpan = praiseSpan;
    }

    public SpannableStringBuilder getPraiseSpan() {
        return praiseSpan;
    }

    private SpannableStringBuilder praiseSpan;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getPortrait() {
        return portrait;
    }

    public void setPortrait(String portrait) {
        this.portrait = portrait;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }
}
